package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.BODY;
import com.util.ConfigUtil;
import com.util.JSONUtil;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

/**
 * 缴存历史记录
 * @author 
 */
@Controller
@RequestMapping("/grtqmxcontroller")
public class grtqmxcontroller {

	private static final Logger logger = Logger.getLogger(grtqmxcontroller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	  
	@RequestMapping("/test")
	@ResponseBody
	public String entryinterface(HttpServletRequest request, HttpServletResponse response , String limit,HttpSession session,String id,String start,String end,String sj) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
    
		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		String sj1[]=sj.split(" - ");
		if(sj1[0]!=null)start=sj1[0].toString().replaceAll("-","");
		if(sj1[1]!=null)end=sj1[1].toString().replaceAll("-", "");
		start+="01";
		end+="31";
		Map reqMap = new HashMap();
		OpenKey opk=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ121");
		//接口参数
		reqMap.put("OpenKey",sessionIn.getKEY());
		BODY b=new BODY();
		b.setGRZH(sessionIn.getGRZH());
		b.setQSRQ(start);
		b.setZZRQ(end);
		reqMap.put("BODY",b);
		//请求
		List retlist=new ArrayList();
		List l=new ArrayList();
		String  result = opk.getOpenKey(reqMap, session);
		Map retmap=JSONUtil.toMap(result);
		if(retmap.get("RETURNCODE").equals("100")){
			Map resMap=(Map) retmap.get("body");
			 retlist=(List) resMap.get("RETDATA");
			for(int i=0;i<retlist.size();i++){
				Map map= (Map) retlist.get(i);
				if(resMap.get("XINGMING").toString()!=null && resMap.get("XINGMING").toString()!=""){
					map.put("XINGMING",resMap.get("XINGMING").toString());
				}
				
			}
		}
		//===================================提取办理进度========================================//
				Map reqMap2 = new HashMap();
				OpenKey opk2=new OpenKey();
				BODY b2=new BODY();
				//接口编码
				reqMap2.put("AUTHORITY", "YBGJJ122");
				//接口参数
				b2.setGRZH(sessionIn.getGRZH());
				reqMap2.put("OpenKey",sessionIn.getKEY());
				reqMap2.put("BODY",b2);
				String result2=opk2.getOpenKey(reqMap2, session);
				Map retmap2=JSONUtil.toMap(result2);
				//===================================结束==============================================//
					if(retmap2.get("RETURNCODE").equals("100")){
						Map mx=JSONUtil.toMap(JSONUtil.ToJsongString2(retmap2.get("body")));
						List l2=(List) mx.get("RETDATA");
						
						l.addAll(l2);
						l.addAll(retlist);
					}else{
						l.addAll(retlist);
					}
					System.out.println(l.toString());
					if(l !=null && l.size() >0){
						map.put("code", retmap2.get("RETURNCODE").toString());
						map.put("CW",  retmap2.get("RETURNDESC").toString());
						map.put("data", l);
						map.put("msg", "1");
						response.setHeader("Content-type", "text/html;charset=UTF-8");
						response.setCharacterEncoding("UTF-8");
						String jsonString = JSONUtil.ToJsongString2(map);
						PrintWriter pw = response.getWriter();
						pw.write(jsonString);
					}else{
						map.put("code", 1);
						map.put("msg", "1");
						map.put("CW", "用户没有提取信息");
						response.setHeader("Content-type", "text/html;charset=UTF-8");
						response.setCharacterEncoding("UTF-8");
						String jsonString = JSONUtil.ToJsongString2(map);
						PrintWriter pw = response.getWriter();
						pw.write(jsonString);
						return null;
					}

		return null;
	}
}
